package com.stormrage.edu.serv.student.impl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.student.StuInfoDao;
import com.stormrage.edu.serv.student.StuInfoServ;
import com.stormrage.entity.student.StuInfo;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.FileUtil;

/**
 * @Title: StuInfoServImpl.java
 * @Package com.stormrage.edu.serv.student.impl
 * @Description: 用户资料 Service
 * @author www.stormragetech.com
 * @date 2016年4月18日
 * @version V1.0
 */
@Service
public class StuInfoServImpl implements StuInfoServ {

	private static final Logger logger = LoggerFactory.getLogger(StuInfoServImpl.class);

	@Autowired
	private StuInfoDao stuInfoDao;

	@Override
	public void update(final StuInfo arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuInfoServ 】- 【 Update 】-【 STARTED 】");

		StuInfo info = queryByAcct(arg0.getAcctId());

		if (null == info)
			stuInfoDao.create(arg0);
		else {
			arg0.setId(info.getId());
			stuInfoDao.update(arg0);
		}

		logger.debug(" 【 Serv 】- 【 StuInfoServ 】- 【 Update 】-【 COMPLETED 】");

	}

	@Override
	public void updateImage(StuInfo arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuInfoServ 】- 【 UpdateImage 】-【 STARTED 】");

		StuInfo info = queryByAcct(arg0.getAcctId());
		if (null == info) {
			StormrageTechException.instance(ErrCons.USER_IMG, ErrCons.USER_IMG_MSG, StormrageTechException.ERR_INF,
					"【 StuInfoServ 】-【 UpdateImage 】");
		}

		arg0.setId(info.getId());
		stuInfoDao.updateImage(arg0);

		logger.debug(" 【 Serv 】- 【 StuInfoServ 】- 【 UpdateImage 】-【 COMPLETED 】");

	}

	@Override
	public StuInfo queryByAcct(Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】- 【 StuInfoServ 】- 【 QueryById 】-【 STARTED 】");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("acctId", arg0);

		StuInfo entity = stuInfoDao.queryByAcct(map);

		logger.debug(" 【 Serv 】- 【 StuInfoServ 】- 【 QueryById 】-【 COMPLETED 】");
		return entity;

	}

	@Override
	public String upload(final MultipartFile arg0, final String arg1) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 StuInfoServ 】- 【 Upload 】- 【 STARTED 】");

		String path = FileUtil.initUploadPath(arg1, "image");
		String fileName = FileUtil.saveFile(arg0, path, "image");

		logger.debug(" 【 Service 】- 【 StuInfoServ 】- 【 Upload 】- 【 COMPLETED 】");
		return fileName;

	}

}
